package cn.finyzq.ss.games.config;

import cn.finyzq.ss.games.service.RoomService;
import cn.finyzq.ss.games.web.interceptor.WebSocketInterceptor;
import cn.finyzq.ss.games.web.ws.RoomSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * Create Time: 2018年08月05日 14:52
 *
 * @author fin
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private RoomSocket roomSocket;


    @Bean
    public ServerEndpointExporter serverEndpointExporter(ApplicationContext context) {
        return new ServerEndpointExporter();
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(roomSocket, "/ws/**")
                .addInterceptors(new WebSocketInterceptor())
                .setAllowedOrigins("*");

    }
}
